/*
 * 文件名：StsQaJobCoreServiceImpl7_8.java
 * 版权：Copyright by www.yunshows.com
 * 描述：
 * 修改人：Awoke
 * 修改时间：2020年2月18日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.job.sts.service.impl;


import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import top.infopub.exception.ServiceException;
import top.infopub.job.fia.service.FeeNoteService;
import top.infopub.job.fia.service.OrgBudgetDetailService;
import top.infopub.job.fia.service.OrgBudgetService;
import top.infopub.job.fia.service.ProBudgetDetailService;
import top.infopub.job.fia.service.ProBudgetService;
import top.infopub.job.hr.service.OrgInfoService;
import top.infopub.job.mkt.service.PreProjectService;
import top.infopub.job.sts.service.OrgAreaQaService;
import top.infopub.job.sts.service.OrgProQaService;
import top.infopub.job.sts.service.OrgSuperQaService;
import top.infopub.job.sts.service.StsQaJobCoreService7_8;
import top.infopub.job.util.ParserUtil;
import top.infopub.model.fia.FeeNote;
import top.infopub.model.fia.OrgBudget;
import top.infopub.model.fia.OrgBudgetDetail;
import top.infopub.model.hr.OrgInfo;
import top.infopub.model.mkt.PreProject;
import top.infopub.model.sts.OrgAreaQa;
import top.infopub.model.sts.OrgProQa;
import top.infopub.model.sts.OrgSuperQa;
import top.infopub.tdp.util.BizParaUtil;

import com.google.common.collect.Maps;

/**
 * 经营质量分析报表 数据归集汇总核心service 为给job服务</br>
 * 7=营销费用 8=营销费率
 * @author Awoke
 * @version 2020年2月15日
 * @see StsQaJobCoreService7_8
 * @since
 */
@Service
@Deprecated
public class StsQaJobCoreServiceImpl7_8 implements StsQaJobCoreService7_8 {

    private static final Logger log = LoggerFactory.getLogger(StsQaJobCoreService7_8.class);

    @Autowired
    private OrgInfoService orgInfoService;

    @Autowired
    private OrgSuperQaService orgSuperQaService;

    @Autowired
    private OrgAreaQaService orgAreaQaService;

    @Autowired
    private OrgProQaService orgProQaService;

    @Autowired
    private OrgBudgetService orgBudgetService;

    @Autowired
    private OrgBudgetDetailService orgBudgetDetailService;

    @Autowired
    private ProBudgetService proBudgetService;

    @Autowired
    private ProBudgetDetailService proBudgetDetailService;

    @Autowired
    private PreProjectService preProjectService;

    @Autowired
    private FeeNoteService feeNoteService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void stsQaOfSaleFeeRateJob() {
        // 1. 新增- 项目公司经营质量 数据
        Map<String, Object> proOrgBudgetWhere = Maps.newHashMap();
        proOrgBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        proOrgBudgetWhere.put("processStatus", "9");
        proOrgBudgetWhere.put("orgTypes", "3");// 公司类型 1=总部公司 2=区域公司 3=项目公司
        // 【公司年度预算fia_org_budget】{营销费用}
        List<OrgBudget> proOrgBudgetList = orgBudgetService.selectByMapWhere(proOrgBudgetWhere);
        // 最外层的for循环 使用预算的org反查其实际完成
        for (OrgBudget var : proOrgBudgetList) {
            OrgProQa proQaAdd = new OrgProQa();
            proQaAdd.setHrOrgProId(var.getHrOrgBillId());
            OrgInfo orgPro = orgInfoService.selectByPrimaryKey(var.getHrOrgBillId());// 查询项目公司信息
            proQaAdd.setOrgProName(orgPro.getCompName());
            proQaAdd.setHrOrgAreaId(orgPro.getParentId());
            // 如果公司没有绑定项目 则跳过
            String proCode = orgPro.getProjectCode();
            if (StringUtils.isBlank(proCode)) {
                log.info("项目与公司：{} 未绑定，统计时，skip~", var.getHrOrgBillId());
                continue;
            }
            else {
                proQaAdd.setProCode(proCode);
                PreProject temp = new PreProject();
                temp.setProCode(proCode);
                temp.setProcessStatus("9");
//                proQaAdd.setProName(preProjectService.selectByEntityWhere(temp).get(0).getProName());
            }
            proQaAdd.setAimType(7);// 指标类型 7=营销费用
            proQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            proQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            proQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

            // 当月计划
            Map<String, Object> orgBudgetDetailWhere = Maps.newHashMap();
            orgBudgetDetailWhere.put("orgBudgetId", var.getId());
            orgBudgetDetailWhere.put("months", ParserUtil.getMonthOfPrevMonth());
            OrgBudgetDetail plan = orgBudgetDetailService.selectByMapWhere(orgBudgetDetailWhere).get(
                0);
            proQaAdd.setCurMonthPlan(plan.getSaleFee());// 原始单位：元
            proQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(plan.getSaleFee()));
            proQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(plan.getSaleFee()));
            // 年度计划
            proQaAdd.setYearAllPlan(var.getSaleFeeSum());// 原始单位：元
            proQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(var.getSaleFeeSum()));
            proQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(var.getSaleFeeSum()));

            // 当月实际完成
            FeeNote feeNoteWhere = new FeeNote();
            feeNoteWhere.setHrOrgBillId(proQaAdd.getHrOrgProId());
            feeNoteWhere.setBeeDateMonth(proQaAdd.getStsMonth());
            feeNoteWhere.setNoteType("3");// 3=营销费用
            feeNoteWhere.setState(1);
            List<FeeNote> feeNoteList = feeNoteService.selectByEntityWhere(feeNoteWhere);
            if (feeNoteList == null || feeNoteList.isEmpty()) {
                // 实际当月完成为0
                proQaAdd.setCurMonthFinish(BigDecimal.ZERO);
                proQaAdd.setCurMonthFinishTts(BigDecimal.ZERO);
                proQaAdd.setCurMonthFinishHms(BigDecimal.ZERO);
            }
            else {
                BigDecimal unit = sumOf(feeNoteList);
                // 实际当月完成
                proQaAdd.setCurMonthFinish(unit);
                proQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(unit));
                proQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(unit));
            }
            proQaAdd.setCurMonthRate(proQaAdd.getCurMonthFinish().divide(
                proQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));// 完成率 =实际完成/计划

            Map<String, Object> prevWhere = Maps.newHashMap();
            prevWhere.put("hrOrgProId", proQaAdd.getHrOrgProId());
            prevWhere.put("aimType", 7);// 指标类型 7=营销费用
            prevWhere.put("belongsYear", proQaAdd.getBelongsYear());
            prevWhere.put("belongsMonth", proQaAdd.getBelongsMonth() - 1);// 前一个月
            prevWhere.put("orderCondition", " add_time desc ");
            List<OrgProQa> prevList = orgProQaService.selectByMapWhere(prevWhere);

            // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据
            if (proQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
                proQaAdd.setYearAddupPlan(proQaAdd.getCurMonthPlan());
                proQaAdd.setYearAddupPlanTts(proQaAdd.getCurMonthPlanTts());
                proQaAdd.setYearAddupPlanHms(proQaAdd.getCurMonthPlanHms());
                proQaAdd.setYearAddupFinish(proQaAdd.getCurMonthFinish());
                proQaAdd.setYearAddupFinishTts(proQaAdd.getCurMonthFinishTts());
                proQaAdd.setYearAddupFinishHms(proQaAdd.getCurMonthFinishHms());
                proQaAdd.setYearAddupRate(proQaAdd.getCurMonthRate());
            }
            else {
                OrgProQa prevProQa = prevList.get(0);
                // 累加   当前月累 = 上月月累 + 当前
                proQaAdd.setYearAddupPlan(proQaAdd.getCurMonthPlan().add(
                    prevProQa.getYearAddupPlan()));
                proQaAdd.setYearAddupPlanTts(proQaAdd.getCurMonthPlanTts().add(
                    prevProQa.getYearAddupPlan()));
                proQaAdd.setYearAddupPlanHms(proQaAdd.getCurMonthPlanHms().add(
                    prevProQa.getYearAddupPlan()));
                proQaAdd.setYearAddupFinish(proQaAdd.getCurMonthFinish().add(
                    prevProQa.getYearAddupPlan()));
                proQaAdd.setYearAddupFinishTts(proQaAdd.getCurMonthFinishTts().add(
                    prevProQa.getYearAddupPlan()));
                proQaAdd.setYearAddupFinishHms(proQaAdd.getCurMonthFinishHms().add(
                    prevProQa.getYearAddupPlan()));
                // 计算月累完成率
                proQaAdd.setYearAddupRate(proQaAdd.getYearAddupFinish().divide(
                    proQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }
            // 年度完成率  月累完成/年度计划
            proQaAdd.setYearAllRate(proQaAdd.getYearAddupFinish().divide(
                proQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            proQaAdd.setAddBy("system");
            proQaAdd.setAddTime(new Date());
            proQaAdd.setUptBy("system");
            proQaAdd.setUptTime(new Date());
            orgProQaService.insert(proQaAdd);

            // 以下插入  项目公司 营销费率 = 营销费用合计/自行承接建安额合计
            OrgProQa proQaAddRate = new OrgProQa();
            proQaAddRate.setHrOrgAreaId(proQaAdd.getHrOrgAreaId());
            proQaAddRate.setHrOrgProId(proQaAdd.getHrOrgProId());
            proQaAddRate.setOrgProName(proQaAdd.getOrgProName());
            proQaAddRate.setProCode(proQaAdd.getProCode());
            proQaAddRate.setProName(proQaAdd.getProName());
            proQaAddRate.setAimType(8); // 营销费率
            proQaAddRate.setBelongsYear(proQaAdd.getBelongsYear());
            proQaAddRate.setBelongsMonth(proQaAdd.getBelongsMonth());
            proQaAddRate.setStsMonth(proQaAdd.getStsMonth());
            // 获得对应月份的自行承接建安额
            Map<String, Object> sameCaeWhere = Maps.newHashMap();
            sameCaeWhere.put("hrOrgProId", proQaAdd.getHrOrgProId());
            sameCaeWhere.put("aimType", 1);// 指标类型 1=自行承接建安额
            sameCaeWhere.put("belongsYear", proQaAdd.getBelongsYear());
            sameCaeWhere.put("belongsMonth", proQaAdd.getBelongsMonth());
            sameCaeWhere.put("orderCondition", " add_time desc ");
            List<OrgProQa> sameCaeList = orgProQaService.selectByMapWhere(sameCaeWhere);
            if (sameCaeList == null || sameCaeList.isEmpty()) {
                throw new ServiceException("[项目]经营质量分析指标-营销费率 数据归集时，取不到对应的自行承接建安额~");
            }
            OrgProQa sameCae = sameCaeList.get(0);
            // 营销费率 = 营销费用合计/自行承接建安额
            proQaAddRate.setCurMonthPlan(proQaAdd.getCurMonthPlan().divide(
                sameCae.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            proQaAddRate.setCurMonthPlanTts(proQaAddRate.getCurMonthPlan());
            proQaAddRate.setCurMonthPlanHms(proQaAddRate.getCurMonthPlan());// 三者相等

            proQaAddRate.setCurMonthFinish(proQaAdd.getCurMonthFinish().divide(
                sameCae.getCurMonthFinish(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            proQaAddRate.setCurMonthFinishTts(proQaAddRate.getCurMonthFinish());
            proQaAddRate.setCurMonthFinishHms(proQaAddRate.getCurMonthFinish());// 三者相等

            proQaAddRate.setYearAddupPlan(proQaAdd.getYearAddupPlan().divide(
                sameCae.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            proQaAddRate.setYearAddupPlanTts(proQaAddRate.getYearAddupPlan());
            proQaAddRate.setYearAddupPlanHms(proQaAddRate.getYearAddupPlan());// 三者相等

            proQaAddRate.setYearAddupFinish(proQaAdd.getYearAddupFinish().divide(
                sameCae.getYearAddupFinish(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            proQaAddRate.setYearAddupFinishTts(proQaAddRate.getYearAddupFinish());
            proQaAddRate.setYearAddupFinishHms(proQaAddRate.getYearAddupFinish());// 三者相等

            proQaAddRate.setYearAllPlan(proQaAdd.getYearAllPlan().divide(sameCae.getYearAllPlan(),
                4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
            proQaAddRate.setYearAllPlanTts(proQaAddRate.getYearAllPlan());
            proQaAddRate.setYearAllPlanHms(proQaAddRate.getYearAllPlan());// 三者相等

            proQaAddRate.setAddBy("system");
            proQaAddRate.setAddTime(new Date());
            proQaAddRate.setUptBy("system");
            proQaAddRate.setUptTime(new Date());
            orgProQaService.insert(proQaAddRate);
        }
        // 2. 新增- 区域公司经营质量 数据
        Map<String, Object> areaOrgBudgetWhere = Maps.newHashMap();
        areaOrgBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        areaOrgBudgetWhere.put("processStatus", "9");
        areaOrgBudgetWhere.put("orgTypes", "2");// 公司类型 1=总部公司 2=区域公司 3=项目公司
        // 【公司年度预算fia_org_budget】{营销费用}
        List<OrgBudget> areaOrgBudgetList = orgBudgetService.selectByMapWhere(areaOrgBudgetWhere);
        // 最外层的for循环 使用预算的org反查其实际完成
        for (OrgBudget var : areaOrgBudgetList) {
            OrgAreaQa areaQaAdd = new OrgAreaQa();
            areaQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
            areaQaAdd.setHrOrgAreaId(var.getHrOrgBillId());
            areaQaAdd.setOrgAreaName(orgInfoService.selectByPrimaryKey(var.getHrOrgBillId()).getCompName());
            areaQaAdd.setAimType(7);// 指标类型 7=营销费用
            areaQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
            areaQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
            areaQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

            // 当月计划
            Map<String, Object> orgBudgetDetailWhere = Maps.newHashMap();
            orgBudgetDetailWhere.put("orgBudgetId", var.getId());
            orgBudgetDetailWhere.put("months", ParserUtil.getMonthOfPrevMonth());
            OrgBudgetDetail plan = orgBudgetDetailService.selectByMapWhere(orgBudgetDetailWhere).get(
                0);
            areaQaAdd.setCurMonthPlan(plan.getSaleFee());// 原始单位：元
            areaQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(plan.getSaleFee()));
            areaQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(plan.getSaleFee()));
            // 年度计划
            areaQaAdd.setYearAllPlan(var.getSaleFeeSum());// 原始单位：元
            areaQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(var.getSaleFeeSum()));
            areaQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(var.getSaleFeeSum()));

            // 当月实际完成
            FeeNote feeNoteWhere = new FeeNote();
            feeNoteWhere.setHrOrgBillId(areaQaAdd.getHrOrgAreaId());
            feeNoteWhere.setBeeDateMonth(areaQaAdd.getStsMonth());
            feeNoteWhere.setNoteType("3");// 3=营销费用
            feeNoteWhere.setState(1);
            List<FeeNote> feeNoteList = feeNoteService.selectByEntityWhere(feeNoteWhere);
            if (feeNoteList == null || feeNoteList.isEmpty()) {
                // 实际当月完成为0
                areaQaAdd.setCurMonthFinish(BigDecimal.ZERO);
                areaQaAdd.setCurMonthFinishTts(BigDecimal.ZERO);
                areaQaAdd.setCurMonthFinishHms(BigDecimal.ZERO);
            }
            else {
                // 实际当月完成
                BigDecimal unit = sumOf(feeNoteList);
                // 实际当月完成
                areaQaAdd.setCurMonthFinish(unit);
                areaQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(unit));
                areaQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(unit));
            }
            areaQaAdd.setCurMonthRate(areaQaAdd.getCurMonthFinish().divide(
                areaQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));// 完成率 =实际完成/计划

            Map<String, Object> prevWhere = Maps.newHashMap();
            prevWhere.put("hrOrgAreaId", areaQaAdd.getHrOrgAreaId());
            prevWhere.put("aimType", 7);// 指标类型 7=营销费用
            prevWhere.put("belongsYear", areaQaAdd.getBelongsYear());
            prevWhere.put("belongsMonth", areaQaAdd.getBelongsMonth() - 1);// 前一个月
            prevWhere.put("orderCondition", " add_time desc ");
            List<OrgAreaQa> prevList = orgAreaQaService.selectByMapWhere(prevWhere);

            // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据
            if (areaQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
                areaQaAdd.setYearAddupPlan(areaQaAdd.getCurMonthPlan());
                areaQaAdd.setYearAddupPlanTts(areaQaAdd.getCurMonthPlanTts());
                areaQaAdd.setYearAddupPlanHms(areaQaAdd.getCurMonthPlanHms());
                areaQaAdd.setYearAddupFinish(areaQaAdd.getCurMonthFinish());
                areaQaAdd.setYearAddupFinishTts(areaQaAdd.getCurMonthFinishTts());
                areaQaAdd.setYearAddupFinishHms(areaQaAdd.getCurMonthFinishHms());
                areaQaAdd.setYearAddupRate(areaQaAdd.getCurMonthRate());
            }
            else {
                OrgAreaQa prevAreaQa = prevList.get(0);
                // 累加   当前月累 = 上月月累 + 当前
                areaQaAdd.setYearAddupPlan(areaQaAdd.getCurMonthPlan().add(
                    prevAreaQa.getYearAddupPlan()));
                areaQaAdd.setYearAddupPlanTts(areaQaAdd.getCurMonthPlanTts().add(
                    prevAreaQa.getYearAddupPlan()));
                areaQaAdd.setYearAddupPlanHms(areaQaAdd.getCurMonthPlanHms().add(
                    prevAreaQa.getYearAddupPlan()));
                areaQaAdd.setYearAddupFinish(areaQaAdd.getCurMonthFinish().add(
                    prevAreaQa.getYearAddupPlan()));
                areaQaAdd.setYearAddupFinishTts(areaQaAdd.getCurMonthFinishTts().add(
                    prevAreaQa.getYearAddupPlan()));
                areaQaAdd.setYearAddupFinishHms(areaQaAdd.getCurMonthFinishHms().add(
                    prevAreaQa.getYearAddupPlan()));
                // 计算月累完成率
                areaQaAdd.setYearAddupRate(areaQaAdd.getYearAddupFinish().divide(
                    areaQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                    ParserUtil.HUNDRED));// 完成率 =实际完成/计划
            }
            // 年度完成率  月累完成/年度计划
            areaQaAdd.setYearAllRate(areaQaAdd.getYearAddupFinish().divide(
                areaQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            areaQaAdd.setAddBy("system");
            areaQaAdd.setAddTime(new Date());
            areaQaAdd.setUptBy("system");
            areaQaAdd.setUptTime(new Date());
            orgAreaQaService.insert(areaQaAdd);

            // 以下插入  区域公司 营销费率 = 营销费用合计/自行承接建安额合计
            OrgAreaQa areaQaAddRate = new OrgAreaQa();
            areaQaAddRate.setHrOrgSuperId(areaQaAdd.getHrOrgSuperId());
            areaQaAddRate.setHrOrgAreaId(areaQaAdd.getHrOrgAreaId());
            areaQaAddRate.setOrgAreaName(areaQaAdd.getOrgAreaName());
            areaQaAddRate.setAimType(8); // 营销费率
            areaQaAddRate.setBelongsYear(areaQaAdd.getBelongsYear());
            areaQaAddRate.setBelongsMonth(areaQaAdd.getBelongsMonth());
            areaQaAddRate.setStsMonth(areaQaAdd.getStsMonth());
            // 获得对应月份的自行承接建安额
            Map<String, Object> sameCaeWhere = Maps.newHashMap();
            sameCaeWhere.put("hrOrgAreaId", areaQaAdd.getHrOrgAreaId());
            sameCaeWhere.put("aimType", 1);// 指标类型 1=自行承接建安额
            sameCaeWhere.put("belongsYear", areaQaAdd.getBelongsYear());
            sameCaeWhere.put("belongsMonth", areaQaAdd.getBelongsMonth());
            sameCaeWhere.put("orderCondition", " add_time desc ");
            List<OrgAreaQa> sameCaeList = orgAreaQaService.selectByMapWhere(sameCaeWhere);
            if (sameCaeList == null || sameCaeList.isEmpty()) {
                throw new ServiceException("[区域]经营质量分析指标-营销费率 数据归集时，取不到对应的自行承接建安额~");
            }
            OrgAreaQa sameCae = sameCaeList.get(0);
            // 营销费率 = 营销费用合计/自行承接建安额
            areaQaAddRate.setCurMonthPlan(areaQaAdd.getCurMonthPlan().divide(
                sameCae.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            areaQaAddRate.setCurMonthPlanTts(areaQaAddRate.getCurMonthPlan());
            areaQaAddRate.setCurMonthPlanHms(areaQaAddRate.getCurMonthPlan());// 三者相等

            areaQaAddRate.setCurMonthFinish(areaQaAdd.getCurMonthFinish().divide(
                sameCae.getCurMonthFinish(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            areaQaAddRate.setCurMonthFinishTts(areaQaAddRate.getCurMonthFinish());
            areaQaAddRate.setCurMonthFinishHms(areaQaAddRate.getCurMonthFinish());// 三者相等

            areaQaAddRate.setYearAddupPlan(areaQaAdd.getYearAddupPlan().divide(
                sameCae.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            areaQaAddRate.setYearAddupPlanTts(areaQaAddRate.getYearAddupPlan());
            areaQaAddRate.setYearAddupPlanHms(areaQaAddRate.getYearAddupPlan());// 三者相等

            areaQaAddRate.setYearAddupFinish(areaQaAdd.getYearAddupFinish().divide(
                sameCae.getYearAddupFinish(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));
            areaQaAddRate.setYearAddupFinishTts(areaQaAddRate.getYearAddupFinish());
            areaQaAddRate.setYearAddupFinishHms(areaQaAddRate.getYearAddupFinish());// 三者相等

            areaQaAddRate.setYearAllPlan(areaQaAdd.getYearAllPlan().divide(
                sameCae.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
            areaQaAddRate.setYearAllPlanTts(areaQaAddRate.getYearAllPlan());
            areaQaAddRate.setYearAllPlanHms(areaQaAddRate.getYearAllPlan());// 三者相等

            areaQaAddRate.setAddBy("system");
            areaQaAddRate.setAddTime(new Date());
            areaQaAddRate.setUptBy("system");
            areaQaAddRate.setUptTime(new Date());
            orgAreaQaService.insert(areaQaAddRate);
        }
        // 3. 新增- 总部公司经营质量 数据
        Map<String, Object> superOrgBudgetWhere = Maps.newHashMap();
        superOrgBudgetWhere.put("hrOrgBillId", BizParaUtil.get("super_org_id"));// 总部公司ID
        superOrgBudgetWhere.put("belongsYear", ParserUtil.getYearOfPrevMonth());
        superOrgBudgetWhere.put("processStatus", "9");
        superOrgBudgetWhere.put("orgTypes", "1");// 公司类型 1=总部公司 2=区域公司 3=项目公司
        // 【公司年度预算fia_org_budget】{营销费用}
        List<OrgBudget> superOrgBudgetList = orgBudgetService.selectByMapWhere(superOrgBudgetWhere);
        // 预算不存在
        if (superOrgBudgetList == null || superOrgBudgetList.isEmpty()) {
            throw new ServiceException("[总部]经营质量分析指标-营销费用 数据归集时，公司预算不存在~");
        }
        OrgBudget var = superOrgBudgetList.get(0);

        OrgSuperQa superQaAdd = new OrgSuperQa();
        superQaAdd.setHrOrgSuperId(BizParaUtil.get("super_org_id"));// 总部公司ID
        superQaAdd.setAimType(7);// 指标类型 7=营销费用
        superQaAdd.setBelongsYear(ParserUtil.getYearOfPrevMonth());
        superQaAdd.setBelongsMonth(ParserUtil.getMonthOfPrevMonth());
        superQaAdd.setStsMonth(ParserUtil.prevMonth());// 上个月

        // 当月计划
        Map<String, Object> orgBudgetDetailWhere = Maps.newHashMap();
        orgBudgetDetailWhere.put("orgBudgetId", var.getId());
        orgBudgetDetailWhere.put("months", ParserUtil.getMonthOfPrevMonth());
        OrgBudgetDetail plan = orgBudgetDetailService.selectByMapWhere(orgBudgetDetailWhere).get(0);
        superQaAdd.setCurMonthPlan(plan.getSaleFee());// 原始单位：元
        superQaAdd.setCurMonthPlanTts(ParserUtil.unit2Tts(plan.getSaleFee()));
        superQaAdd.setCurMonthPlanHms(ParserUtil.unit2Hms(plan.getSaleFee()));
        // 年度计划
        superQaAdd.setYearAllPlan(var.getSaleFeeSum());// 原始单位：元
        superQaAdd.setYearAllPlanTts(ParserUtil.unit2Tts(var.getSaleFeeSum()));
        superQaAdd.setYearAllPlanHms(ParserUtil.unit2Hms(var.getSaleFeeSum()));

        // 当月实际完成
        FeeNote feeNoteWhere = new FeeNote();
        feeNoteWhere.setHrOrgBillId(superQaAdd.getHrOrgSuperId());
        feeNoteWhere.setBeeDateMonth(superQaAdd.getStsMonth());
        feeNoteWhere.setNoteType("3");// 3=营销费用
        feeNoteWhere.setState(1);
        List<FeeNote> feeNoteList = feeNoteService.selectByEntityWhere(feeNoteWhere);
        if (feeNoteList == null || feeNoteList.isEmpty()) {
            // 实际当月完成为0
            superQaAdd.setCurMonthFinish(BigDecimal.ZERO);
            superQaAdd.setCurMonthFinishTts(BigDecimal.ZERO);
            superQaAdd.setCurMonthFinishHms(BigDecimal.ZERO);
        }
        else {
            BigDecimal unit = sumOf(feeNoteList);
            // 实际当月完成
            superQaAdd.setCurMonthFinish(unit);
            superQaAdd.setCurMonthFinishTts(ParserUtil.unit2Tts(unit));
            superQaAdd.setCurMonthFinishHms(ParserUtil.unit2Hms(unit));
        }
        superQaAdd.setCurMonthRate(superQaAdd.getCurMonthFinish().divide(
            superQaAdd.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));// 完成率 =实际完成/计划

        Map<String, Object> prevWhere = Maps.newHashMap();
        prevWhere.put("hrOrgSuperId", BizParaUtil.get("super_org_id"));// 总部公司ID
        prevWhere.put("aimType", 7);// 指标类型 7=营销费用
        prevWhere.put("belongsYear", superQaAdd.getBelongsYear());
        prevWhere.put("belongsMonth", superQaAdd.getBelongsMonth() - 1);// 前一个月
        prevWhere.put("orderCondition", " add_time desc ");
        List<OrgSuperQa> prevList = orgSuperQaService.selectByMapWhere(prevWhere);

        // 月累数据   如果是当前年份的第一个月  或者 没有此前月累数据，则月累数据=当月数据  否则需要取前一个月的月累数据+当月数据
        if (superQaAdd.getBelongsMonth() == 1 || prevList == null || prevList.isEmpty()) {
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan());
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts());
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms());
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish());
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts());
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms());
            superQaAdd.setYearAddupRate(superQaAdd.getCurMonthRate());
        }
        else {
            OrgSuperQa prevSuperQa = prevList.get(0);
            // 累加   当前月累 = 上月月累 + 当前
            superQaAdd.setYearAddupPlan(superQaAdd.getCurMonthPlan().add(
                prevSuperQa.getYearAddupPlan()));
            superQaAdd.setYearAddupPlanTts(superQaAdd.getCurMonthPlanTts().add(
                prevSuperQa.getYearAddupPlan()));
            superQaAdd.setYearAddupPlanHms(superQaAdd.getCurMonthPlanHms().add(
                prevSuperQa.getYearAddupPlan()));
            superQaAdd.setYearAddupFinish(superQaAdd.getCurMonthFinish().add(
                prevSuperQa.getYearAddupPlan()));
            superQaAdd.setYearAddupFinishTts(superQaAdd.getCurMonthFinishTts().add(
                prevSuperQa.getYearAddupPlan()));
            superQaAdd.setYearAddupFinishHms(superQaAdd.getCurMonthFinishHms().add(
                prevSuperQa.getYearAddupPlan()));
            // 计算月累完成率
            superQaAdd.setYearAddupRate(superQaAdd.getYearAddupFinish().divide(
                superQaAdd.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(
                ParserUtil.HUNDRED));// 完成率 =实际完成/计划
        }
        // 年度完成率  月累完成/年度计划
        superQaAdd.setYearAllRate(superQaAdd.getYearAddupFinish().divide(
            superQaAdd.getYearAllPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
        superQaAdd.setAddBy("system");
        superQaAdd.setAddTime(new Date());
        superQaAdd.setUptBy("system");
        superQaAdd.setUptTime(new Date());
        orgSuperQaService.insert(superQaAdd);

        // 以下插入  总部公司 营销费率 = 营销费用合计/自行承接建安额合计
        OrgSuperQa superQaAddRate = new OrgSuperQa();
        superQaAddRate.setHrOrgSuperId(superQaAdd.getHrOrgSuperId());
        superQaAddRate.setAimType(8); // 营销费率
        superQaAddRate.setBelongsYear(superQaAdd.getBelongsYear());
        superQaAddRate.setBelongsMonth(superQaAdd.getBelongsMonth());
        superQaAddRate.setStsMonth(superQaAdd.getStsMonth());
        // 获得对应月份的自行承接建安额
        Map<String, Object> sameCaeWhere = Maps.newHashMap();
        sameCaeWhere.put("hrOrgSuperId", superQaAdd.getHrOrgSuperId());
        sameCaeWhere.put("aimType", 1);// 指标类型 1=自行承接建安额
        sameCaeWhere.put("belongsYear", superQaAdd.getBelongsYear());
        sameCaeWhere.put("belongsMonth", superQaAdd.getBelongsMonth());
        sameCaeWhere.put("orderCondition", " add_time desc ");
        List<OrgAreaQa> sameCaeList = orgAreaQaService.selectByMapWhere(sameCaeWhere);
        if (sameCaeList == null || sameCaeList.isEmpty()) {
            throw new ServiceException("[总部]经营质量分析指标-营销费率 数据归集时，取不到对应的自行承接建安额~");
        }
        OrgAreaQa sameCae = sameCaeList.get(0);
        // 营销费率 = 营销费用合计/自行承接建安额
        superQaAddRate.setCurMonthPlan(superQaAdd.getCurMonthPlan().divide(
            sameCae.getCurMonthPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
        superQaAddRate.setCurMonthPlanTts(superQaAddRate.getCurMonthPlan());
        superQaAddRate.setCurMonthPlanHms(superQaAddRate.getCurMonthPlan());// 三者相等

        superQaAddRate.setCurMonthFinish(superQaAdd.getCurMonthFinish().divide(
            sameCae.getCurMonthFinish(), 4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
        superQaAddRate.setCurMonthFinishTts(superQaAddRate.getCurMonthFinish());
        superQaAddRate.setCurMonthFinishHms(superQaAddRate.getCurMonthFinish());// 三者相等

        superQaAddRate.setYearAddupPlan(superQaAdd.getYearAddupPlan().divide(
            sameCae.getYearAddupPlan(), 4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
        superQaAddRate.setYearAddupPlanTts(superQaAddRate.getYearAddupPlan());
        superQaAddRate.setYearAddupPlanHms(superQaAddRate.getYearAddupPlan());// 三者相等

        superQaAddRate.setYearAddupFinish(superQaAdd.getYearAddupFinish().divide(
            sameCae.getYearAddupFinish(), 4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
        superQaAddRate.setYearAddupFinishTts(superQaAddRate.getYearAddupFinish());
        superQaAddRate.setYearAddupFinishHms(superQaAddRate.getYearAddupFinish());// 三者相等

        superQaAddRate.setYearAllPlan(superQaAdd.getYearAllPlan().divide(sameCae.getYearAllPlan(),
            4, BigDecimal.ROUND_HALF_UP).multiply(ParserUtil.HUNDRED));
        superQaAddRate.setYearAllPlanTts(superQaAddRate.getYearAllPlan());
        superQaAddRate.setYearAllPlanHms(superQaAddRate.getYearAllPlan());// 三者相等

        superQaAddRate.setAddBy("system");
        superQaAddRate.setAddTime(new Date());
        superQaAddRate.setUptBy("system");
        superQaAddRate.setUptTime(new Date());
        orgSuperQaService.insert(superQaAddRate);
    }

    /**
     * 合计费用
     */
    private BigDecimal sumOf(List<FeeNote> feeNoteList) {
        BigDecimal unit = BigDecimal.ZERO;
        for (FeeNote var : feeNoteList) {
            unit = unit.add(var.getBeeAmount());
        }
        return unit;
    }

}
